package com.sywb.chuangyebao.contract;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.view.fragment.UserDynamicFragment;
import com.sywb.chuangyebao.view.fragment.NewsFragment;
import com.sywb.chuangyebao.view.fragment.UserVideoFragment;

import org.bining.footstone.adapter.BasePagerFragmentAdapter;
import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;

import java.util.ArrayList;
import java.util.List;

public interface UserIndexContract {

    interface View extends IView {

        FragmentManager getUserFragmentManager();

        ViewPager getViewpager();

        int getUid();

        void setSelectPageItem(int postion);
    }

    class Presenter extends BasePresenter<View> {

        private ViewPager viewPager;
        private int uid;
        private int userRole;

        private List<String> mTitleDatas;
        private List<Fragment> mFragments;
        private FragmentPagerAdapter fragmentPagerAdapter;

        @Override
        public void onStart() {
            viewPager = mView.getViewpager();
            uid = mView.getUid();
        }

        public void initPageData(int userRole) {
            this.userRole = userRole;

            mTitleDatas = new ArrayList<>();
            mFragments = new ArrayList<>();

            mTitleDatas.add("动态");
            mFragments.add(UserDynamicFragment.newInstance(uid));
            switch (userRole) {
                case Constants.USER_TYPE_NORMAL:
                    mTitleDatas.add("文章");
                    mFragments.add(NewsFragment.newInstance(uid));
                    mTitleDatas.add("视频");
                    mFragments.add(UserVideoFragment.newInstance(uid));
                    break;
                case Constants.USER_TYPE_PROJECT:
                    mTitleDatas.add("文章");
                    mFragments.add(NewsFragment.newInstance(uid));
                    mTitleDatas.add("视频");
                    mFragments.add(UserVideoFragment.newInstance(uid));
                    mTitleDatas.add("话题");
                    mTitleDatas.add("官网");
                    break;
                case Constants.USER_TYPE_SERVICE:
                    mTitleDatas.add("服务");
                    break;
            }

            fragmentPagerAdapter = new MyPagerFragmentAdapter(mView.getUserFragmentManager(), mFragments, mTitleDatas);

            // recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
            // recyclerView.setAdapter(tutorAdapter);

            viewPager.setAdapter(fragmentPagerAdapter);
            viewPager.setOffscreenPageLimit(mFragments.size());

            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    mView.setSelectPageItem(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
        }

        public void setCurrentPage(int postion) {
            viewPager.setCurrentItem(postion, false);
        }

    }

    class MyPagerFragmentAdapter extends BasePagerFragmentAdapter {

        private List<String> titles;

        public MyPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> titles) {
            super(fragmentManager, fragments);
            this.titles = titles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

}

